package com.java2007.zhangyu.restaurant.controller.front;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.java2007.zhangyu.restaurant.constant.ServletConstant;
import com.java2007.zhangyu.restaurant.controller.BaseServlet;
import com.java2007.zhangyu.restaurant.entity.CartItem;
import com.java2007.zhangyu.restaurant.service.CartService;
import com.java2007.zhangyu.restaurant.service.impl.CartServiceImpl;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 购物车控制器
 * @author zy
 * @Version
 * @date 2021/01/29
 */
@WebServlet("/shoppingcart")
public class ShoppingCartController extends BaseServlet {

    private CartService cartService = new CartServiceImpl();

    /**
     * 根据菜品 id删除购物车的数据
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String deleteByFoodId(HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession();

        List<CartItem> cart = (List<CartItem>) session.getAttribute("front_cart");

        if(null == cart || cart.size() <= 0) {
            return "购物车为空！！！";
        }

        String fIdStr = request.getParameter("fId");
        int fId = Integer.parseInt(fIdStr);
        for (CartItem cartItem : cart) {
            if (cartItem.getfId().intValue() == fId) {
                cart.remove(cartItem);
                break;
            }
        }

        Long cartTotalPrice = 0L;
        for (CartItem cartItem : cart) {
            cartTotalPrice += cartItem.getTotalPrice();
        }
        session.setAttribute("front_cart", cart);
        session.setAttribute("cart_total_price", cartTotalPrice);
        Map<String,Object> map = new HashMap<>();
        map.put("tprice", cartTotalPrice);
        map.put("success", true);

        String json = JSON.toJSONString(map);


        return json;
    }

    /**
     * 加入餐车
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession();
        //获取请求的参数，菜品id、菜品数量
        String fIdStr = request.getParameter("fId");
        String numStr = request.getParameter("num");

        if(!StringUtils.isEmpty(fIdStr) && !StringUtils.isEmpty(numStr)){

            List<CartItem> cart = (List<CartItem>) session.getAttribute("front_cart");
            if(null == cart ) {
                cart = new ArrayList<>();
            }
            //转换为整型
            int fId = Integer.parseInt(fIdStr);
            int num = Integer.parseInt(numStr);

            cart = cartService.add(fId, num, cart);
            session.setAttribute("front_cart", cart);

            Long cartTotalPrice = 0L;

            for (CartItem cartItem : cart) {
                cartTotalPrice += cartItem.getTotalPrice();
            }
            session.setAttribute("cart_total_price", cartTotalPrice);

            return ServletConstant.PRE_REDIRECT + "/front/detail/clientCart.jsp";
        } else {
            return "错误操作！！!";
        }
    }

    /**
     * 修改购物车中的数据
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String changeNum(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        List<CartItem> cart = (List<CartItem>) session.getAttribute("front_cart");

        if(null == cart || cart.size() <= 0) {
            return "修改错误";
        }

        String fIdStr = request.getParameter("fId");
        String numStr = request.getParameter("num");
        int fId = Integer.parseInt(fIdStr);

        session.setAttribute("food_Id",fId);
        int num = Integer.parseInt(numStr);

        long totalPrice = 0L;

        for (CartItem cartItem : cart) {
            if (cartItem.getfId().intValue() == fId) {
                cartItem.setNum(num);
                cartItem.setTotalPrice(cartItem.getfPrice()*cartItem.getNum());
                totalPrice = cartItem.getNum() * cartItem.getfPrice();
                break;
            }
        }

        Double cartTotalPrice = 0.0;
        for (CartItem cartItem : cart) {
            cartTotalPrice += cartItem.getTotalPrice();
        }
        session.setAttribute("front_cart", cart);
        session.setAttribute("cart_total_price", cartTotalPrice);

        Map<String,Object> map = new HashMap<>();

        map.put("sameFoodPrice",totalPrice);
        map.put("tprice", cartTotalPrice);
        map.put("success", true);

        String json = JSON.toJSONString(map);

        return json;
    }
}
